#include "server.hpp"

void Onconnected(const PtrConnection& conn)
{
    cout<<"in Onconnected"<<endl;
    DBG_LOG("connected: %d", conn->Fd());
    
}

void OnMessage(const PtrConnection& conn, Buffer* buf)//处理接收到的消息
{
    DBG_LOG("message: %d", conn->Fd());
   char buff[1024]={0};
   buf->ReadandPop(buff, buf->Readablesize());//从输入缓冲区读取数据到buff中
   DBG_LOG("received data: %s", buff);
  
   std::string str="hello world";
   conn->Send(str.c_str(), str.size());//发送字符串到客户端
}

void connectionDestroy(const PtrConnection& conn)
{
    DBG_LOG("connection destroy: %d", conn->Fd());
    // 连接销毁时的处理逻辑
}

int main()
{
    Tcpserver server(8501);//创建一个Tcpserver对象，绑定到8501端口
    server.SetThreadCount(2);//设置线程池的线程个数
    server.EnableInactiveRelease(10);
    server.SetConnectedCallback(Onconnected);//设置连接回调函数
    server.SetMessageCallback(OnMessage);//设置消息回调函数
    server.SetCloseCallback(connectionDestroy);//设置连接销毁回调函数
    server.Start();

    return 0;
}